package com.block.source.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.exception.NotifyExceptionService;
import com.block.common.service.LogFactory;
import com.block.db.entity.SourceBaseInfo;
import com.block.db.entity.SourceDataAddup;
import com.block.db.entity.SourceRecordInfo;
import com.block.db.mapper.SourceDataAddupMapper;
import com.block.source.enums.DataActionEnum;
import com.block.source.service.ISourceDataAddupService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2023-05-28
 */
@Service
public class SourceDataAddupServiceImpl extends ServiceImpl<SourceDataAddupMapper, SourceDataAddup> implements ISourceDataAddupService {

    private static Logger log = LogFactory.getSourceLog();
    @Autowired
    NotifyExceptionService notifyExceptionService;

    @Async
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public boolean saveOrUpdateDataAddupInfo(String dateInfo, DataActionEnum dataActionEnum, SourceBaseInfo sourceBaseInfo, SourceRecordInfo recordInfo){

        try {
            SourceDataAddup sourceDataAddup = this.baseMapper.getEntityByDateInfoAndBaseIdLock(dateInfo, sourceBaseInfo.getId());
            if (ObjectUtil.isEmpty(sourceDataAddup)) {
                try {
                    sourceDataAddup = this.saveNewEntity(dateInfo, sourceBaseInfo);
                    if (ObjectUtil.isEmpty(sourceDataAddup)) {
                        log.info("【数据推送统计】新增统计信息为空【dateInfo】{}【configId】{}", dateInfo, sourceBaseInfo.getId());
                        return false;
                    }
                } catch (Exception exception) {
                    log.error("【数据推送统计】新增统计信息异常", exception);
                    String content = StrUtil.format("【dateInfo】{}【configId】{}", dateInfo, sourceBaseInfo.getId());
                    notifyExceptionService.notifyNoticeInfo("SOURCE", "新增统计信息异常", content, exception);
                }
            }

            SourceDataAddup updateEntity = new SourceDataAddup();
            updateEntity.setId(sourceDataAddup.getId());
            updateEntity.setUpdateTime(LocalDateTime.now());
            /**
             * 根据操作类型更新数据信息
             */
            if (StrUtil.equals(dataActionEnum.getValue(), DataActionEnum.CHECK.getValue())) {
                updateEntity.setCheckNumber(1);
            } else if (StrUtil.equals(dataActionEnum.getValue(), DataActionEnum.CANPUSH.getValue())) {
                updateEntity.setCheckNumber(1);
                updateEntity.setCanpushNumber(1);
            } else if (StrUtil.equals(dataActionEnum.getValue(), DataActionEnum.REGISTER.getValue())) {
                // 注册成功，没有推送成功，不结算
                updateEntity.setRegisterNumber(1);
            } else if (StrUtil.equals(dataActionEnum.getValue(), DataActionEnum.PUSH.getValue())) {
                // 推送成功，需要计算
                updateEntity.setPushNumber(1);
                updateEntity.setCostAmount(recordInfo.getBaseAmount());
                updateEntity.setProfitAmount(recordInfo.getPushPriceAmount());
            }

            int updateRes = this.baseMapper.updateEntityById(updateEntity);
            if (updateRes > 0) {
                return true;
            }
            return false;
        } catch (Exception exception) {
            log.error("【数据推送统计】线索统计异常", exception);
            String content = StrUtil.format("【dateInfo】{}【baseId】{}", dateInfo, sourceBaseInfo.getId());
            notifyExceptionService.notifyNoticeInfo("SOURCE", "线索统计异常", content, exception);
        }
        return false;
    }

    private SourceDataAddup saveNewEntity(String dateInfo, SourceBaseInfo sourceBaseInfo) {
        SourceDataAddup newEntity = new SourceDataAddup();
        newEntity.setDateInfo(dateInfo);
        newEntity.setBaseId(sourceBaseInfo.getId());
        newEntity.setBaseName(sourceBaseInfo.getChannelName());

        boolean save = this.save(newEntity);
        if (!save) {
            return null;
        }
        return this.baseMapper.getEntityByDateInfoAndBaseIdLock(dateInfo, sourceBaseInfo.getId());
    }

}
